/*
 * Copyright (C) 2014 Andrey Kulikov (andkulikov@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.transitionseverywhere.utils;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.StackLayout;
import ohos.agp.components.element.Element;
import ohos.app.Context;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Andrey Kulikov on 15.11.14.
 */
class ViewOverlayPreJellybean extends StackLayout {

    private static final Field FIELD_VIEW_PARENT = ReflectionUtils.getPrivateField(Component.class, "mParent");

    private List<Element> mDrawableOverlays;

    public ViewOverlayPreJellybean(Context context) {
        super(context);
        init();
    }

    public ViewOverlayPreJellybean(Context context, AttrSet attrs) {
        super(context, attrs);
        init();
    }

    public ViewOverlayPreJellybean(Context context, AttrSet attrs, String styleName) {
        super(context, attrs, styleName);
        init();
    }

    private void init() {
        mDrawableOverlays = new ArrayList<Element>();
    }

    @Override
    public void addComponent(Component child, int left, int top) {
        if (child.getComponentParent() instanceof ComponentContainer) {
            ComponentContainer parent = (ComponentContainer) child.getComponentParent();

            /*LayoutTransition layoutTransition = null;
            if (parent.getLayoutTransition() != null) {
                layoutTransition = parent.getLayoutTransition();
                parent.setLayoutTransition(null);
            }*/
            parent.removeComponent(child);
           /* if (layoutTransition != null) {
                parent.setLayoutTransition(layoutTransition);
            }*/

            if (child.getComponentParent() != null) {
                // LayoutTransition will cause the child to delay removal - cancel it
                ViewGroupUtils.cancelLayoutTransition(parent);
                // fail-safe if view is still attached for any reason
                if (child.getComponentParent() != null && FIELD_VIEW_PARENT != null) {
                    ReflectionUtils.setFieldValue(child, FIELD_VIEW_PARENT, null);
                }
            }
            if (child.getComponentParent() != null) {
                return;
            }
        }
        child.setTag(child.getLayoutConfig());
        addComponent(child, initParams(child, left, top));
        invalidate();
    }

    /*@Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        // Intercept and noop all touch events - overlays do not allow touch events
        return false;
    }*/

    @Override
    public void removeComponent(Component view) {
        super.removeComponent(view);
        view.setTag(null);
    }

    public void moveView(Component view, int left, int top) {
        if (view.getComponentParent() == this) {
            view.setLayoutConfig(initParams(view, left, top));
        }
    }

    private LayoutConfig initParams(Component view, int left, int top) {
        int[] loc = new int[2];
        getLocationOnScreen();

        final LayoutConfig layoutParams = new LayoutConfig(ComponentContainer.LayoutConfig.MATCH_CONTENT,
                ComponentContainer.LayoutConfig.MATCH_CONTENT);
        left -= loc[0];
        top -= loc[1];
        //layoutParams.leftMargin = left;
        // layoutParams.topMargin = top;
        view.setLeft(left);
        view.setTop(top);
        if (view.getWidth() != 0) {
            layoutParams.width = view.getWidth();
            view.setRight(left + layoutParams.width);
        }
        if (view.getWidth() != 0) {
            layoutParams.height = view.getWidth();
            view.setBottom(top + layoutParams.height);
        }
        return layoutParams;
    }

    public synchronized void addDrawable(Element drawable) {
        if (drawable != null) {
            this.mDrawableOverlays.add(drawable);
            invalidate();
        }
    }

    public synchronized void removeDrawable(Element drawable) {
        mDrawableOverlays.remove(drawable);
        invalidate();
    }

    public static ViewOverlayPreJellybean getOverlay(ComponentContainer sceneRoot) {
        if (sceneRoot != null) {
            ComponentContainer group = sceneRoot;
            while (/*group.getId() != openharmony.R.id.content && */group.getComponentParent() != null &&
                    group.getComponentParent() instanceof ComponentContainer) {
                group = (ComponentContainer) group.getComponentParent();
            }
            for (int i = 0; i < group.getChildCount(); i++) {
                Component child = group.getComponentAt(i);
                if (child instanceof ViewOverlayPreJellybean) {
                    return (ViewOverlayPreJellybean) child;
                }
            }
            final ComponentContainer.LayoutConfig params = new LayoutConfig(
                    ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_PARENT);
            ViewOverlayPreJellybean viewOverlay = new ViewOverlayPreJellybean(sceneRoot.getContext());
            group.addComponent(viewOverlay, params);
            return viewOverlay;
        } else {
            return null;
        }
    }

}
